****************
* REQUIREMENTS *
****************

* Stata 17
* outreg2

******************
* APPENDING DATA *
******************

* Note: the 12 txt files need to be saved locally and the the files' path need to be udpated below

cd "/Users/damienbol/Dropbox/Preference for electoral rules/"


set more off

insheet using 180516_1022_prep.txt, tab clear

gen session=1
gen uk=1

save session1.dta, replace

insheet using 180516_1152_prep.txt, tab clear

gen session=2
gen uk=1

save session2.dta, replace

insheet using 180516_1250_prep.txt, tab clear

gen session=3
gen uk=1

save session3.dta, replace

insheet using 180516_1346_prep.txt, tab clear

gen session=4
gen uk=1

save session4.dta, replace

insheet using 180516_1452_prep.txt, tab clear

gen session=5
gen uk=1

save session5.dta, replace

insheet using 180516_1543_prep.txt, tab clear

gen session=6
gen uk=1

save session6.dta, replace

insheet using 180619_1055_prep.txt, tab clear

gen session=7
gen uk=0

save session7.dta, replace

insheet using 180619_1239_prep.txt, tab clear

gen session=8
gen uk=0

save session8.dta, replace

insheet using 180619_1411_prep.txt, tab clear

gen session=9
gen uk=0

save session9.dta, replace

insheet using 180619_1545_prep.txt, tab clear

gen session=10
gen uk=0

save session10.dta, replace

insheet using 180622_1250_prep.txt, tab clear

gen session=11
gen uk=0

save session11.dta, replace

insheet using 180622_1521_prep.txt, tab clear

gen session=12
gen uk=0

save session12.dta, replace

use session1.dta, clear
append using session2.dta, force
append using session3.dta, force
append using session4.dta, force
append using session5.dta, force
append using session6.dta, force
append using session7.dta, force
append using session8.dta, force
append using session9.dta, force
append using session10.dta, force
append using session11.dta, force
append using session12.dta, force

save allsessions.dta, replace

**********************
* Creating variables *
**********************

* Identifying elections with approval and fptp
**********************************************

gen av=0
replace av=1 if electoralsystem==2

* Creating an id variable by subject/session 
********************************************

egen group_id=group(subject session)

* Creating a variable calculating the difference in points that the subject earns under approval and fptp in first part
***********************************************************************************************************************

* Creating a variale capturing number of points earned at each election (first part only) 

gen points=.
replace points=9-distancetowinner if period<9
replace points=0 if points<0

* Differentiating the number of points earned under approval and fptp

bysort session subject: egen points_av=total(points) if av==1 & period<9
bysort session subject: egen points_fptp=total(points) if av==0 & period<9

* Summing up all the points earned during all 4 elections (per electoral system)

bysort session subject: egen points_av_tot=total(points_av)
bysort session subject: egen points_fptp_tot=total(points_fptp)

* Dividing by 4 because points are counted four times

replace points_av_tot=points_av_tot/4
replace points_fptp_tot=points_fptp_tot/4

* Taking the difference between approval and fptp

gen benefit_behavioral=points_av_tot-points_fptp_tot

* Creating a variable calculating the difference in points that the subject's position (and symmetric position) earns under approval and fptp in first part
***********************************************************************************************************************************************************

* Creating a variale capturing number of points earned by each position for at each election  (first part only)

gen points_0=.
gen points_1=.
gen points_2=.
gen points_3=.
gen points_4=.
gen points_5=.
gen points_6=.
gen points_7=.
gen points_8=.
gen points_9=.
gen points_10=.
gen points_11=.
gen points_12=.
gen points_13=.
gen points_14=.
gen points_15=.
gen points_16=.
gen points_17=.
gen points_18=.
gen points_19=.
gen points_20=.

replace points_0=9-(abs(winnerposition-0)) if period<9
replace points_1=9-(abs(winnerposition-1)) if period<9
replace points_2=9-(abs(winnerposition-2)) if period<9
replace points_3=9-(abs(winnerposition-3)) if period<9
replace points_4=9-(abs(winnerposition-4)) if period<9
replace points_5=9-(abs(winnerposition-5)) if period<9
replace points_6=9-(abs(winnerposition-6)) if period<9
replace points_7=9-(abs(winnerposition-7)) if period<9
replace points_8=9-(abs(winnerposition-8)) if period<9
replace points_9=9-(abs(winnerposition-9)) if period<9
replace points_10=9-(abs(winnerposition-10)) if period<9
replace points_11=9-(abs(winnerposition-11)) if period<9
replace points_12=9-(abs(winnerposition-12)) if period<9
replace points_13=9-(abs(winnerposition-13)) if period<9
replace points_14=9-(abs(winnerposition-14)) if period<9
replace points_15=9-(abs(winnerposition-15)) if period<9
replace points_16=9-(abs(winnerposition-16)) if period<9
replace points_17=9-(abs(winnerposition-17)) if period<9
replace points_18=9-(abs(winnerposition-18)) if period<9
replace points_19=9-(abs(winnerposition-19)) if period<9
replace points_20=9-(abs(winnerposition-20)) if period<9

replace points_0=0 if points_0<0
replace points_1=0 if points_1<0
replace points_2=0 if points_2<0
replace points_3=0 if points_3<0
replace points_4=0 if points_4<0
replace points_5=0 if points_5<0
replace points_6=0 if points_6<0
replace points_7=0 if points_7<0
replace points_8=0 if points_8<0
replace points_9=0 if points_9<0
replace points_10=0 if points_10<0
replace points_11=0 if points_11<0
replace points_12=0 if points_12<0
replace points_13=0 if points_13<0
replace points_14=0 if points_14<0
replace points_15=0 if points_15<0
replace points_16=0 if points_16<0
replace points_17=0 if points_17<0
replace points_18=0 if points_18<0
replace points_19=0 if points_19<0
replace points_20=0 if points_20<0

* Differentiating the number of points earned under approval and fptp

bysort session subject: egen points_0_av=total(points_0) if av==1 & period<9
bysort session subject: egen points_1_av=total(points_1) if av==1 & period<9
bysort session subject: egen points_2_av=total(points_2) if av==1 & period<9
bysort session subject: egen points_3_av=total(points_3) if av==1 & period<9
bysort session subject: egen points_4_av=total(points_4) if av==1 & period<9
bysort session subject: egen points_5_av=total(points_5) if av==1 & period<9
bysort session subject: egen points_6_av=total(points_6) if av==1 & period<9
bysort session subject: egen points_7_av=total(points_7) if av==1 & period<9
bysort session subject: egen points_8_av=total(points_8) if av==1 & period<9
bysort session subject: egen points_9_av=total(points_9) if av==1 & period<9
bysort session subject: egen points_10_av=total(points_10) if av==1 & period<9
bysort session subject: egen points_11_av=total(points_11) if av==1 & period<9
bysort session subject: egen points_12_av=total(points_12) if av==1 & period<9
bysort session subject: egen points_13_av=total(points_13) if av==1 & period<9
bysort session subject: egen points_14_av=total(points_14) if av==1 & period<9
bysort session subject: egen points_15_av=total(points_15) if av==1 & period<9
bysort session subject: egen points_16_av=total(points_16) if av==1 & period<9
bysort session subject: egen points_17_av=total(points_17) if av==1 & period<9
bysort session subject: egen points_18_av=total(points_18) if av==1 & period<9
bysort session subject: egen points_19_av=total(points_19) if av==1 & period<9
bysort session subject: egen points_20_av=total(points_20) if av==1 & period<9

bysort session subject: egen points_0_fptp=total(points_0) if av ==0 & period<9
bysort session subject: egen points_1_fptp=total(points_1) if av ==0 & period<9
bysort session subject: egen points_2_fptp=total(points_2) if av ==0 & period<9
bysort session subject: egen points_3_fptp=total(points_3) if av ==0 & period<9
bysort session subject: egen points_4_fptp=total(points_4) if av ==0 & period<9
bysort session subject: egen points_5_fptp=total(points_5) if av ==0 & period<9
bysort session subject: egen points_6_fptp=total(points_6) if av ==0 & period<9
bysort session subject: egen points_7_fptp=total(points_7) if av ==0 & period<9
bysort session subject: egen points_8_fptp=total(points_8) if av ==0 & period<9
bysort session subject: egen points_9_fptp=total(points_9) if av ==0 & period<9
bysort session subject: egen points_10_fptp=total(points_10) if av ==0 & period<9
bysort session subject: egen points_11_fptp=total(points_11) if av ==0 & period<9
bysort session subject: egen points_12_fptp=total(points_12) if av ==0 & period<9
bysort session subject: egen points_13_fptp=total(points_13) if av ==0 & period<9
bysort session subject: egen points_14_fptp=total(points_14) if av ==0 & period<9
bysort session subject: egen points_15_fptp=total(points_15) if av ==0 & period<9
bysort session subject: egen points_16_fptp=total(points_16) if av ==0 & period<9
bysort session subject: egen points_17_fptp=total(points_17) if av ==0 & period<9
bysort session subject: egen points_18_fptp=total(points_18) if av ==0 & period<9
bysort session subject: egen points_19_fptp=total(points_19) if av ==0 & period<9
bysort session subject: egen points_20_fptp=total(points_20) if av ==0 & period<9

* Summing up all the points earned during all 4 elections (per electoral system)

bysort group_id: egen points_0_av_tot=total(points_0_av)
bysort group_id: egen points_1_av_tot=total(points_1_av)
bysort group_id: egen points_2_av_tot=total(points_2_av)
bysort group_id: egen points_3_av_tot=total(points_3_av)
bysort group_id: egen points_4_av_tot=total(points_4_av)
bysort group_id: egen points_5_av_tot=total(points_5_av)
bysort group_id: egen points_6_av_tot=total(points_6_av)
bysort group_id: egen points_7_av_tot=total(points_7_av)
bysort group_id: egen points_8_av_tot=total(points_8_av)
bysort group_id: egen points_9_av_tot=total(points_9_av)
bysort group_id: egen points_10_av_tot=total(points_10_av)
bysort group_id: egen points_11_av_tot=total(points_11_av)
bysort group_id: egen points_12_av_tot=total(points_12_av)
bysort group_id: egen points_13_av_tot=total(points_13_av)
bysort group_id: egen points_14_av_tot=total(points_14_av)
bysort group_id: egen points_15_av_tot=total(points_15_av)
bysort group_id: egen points_16_av_tot=total(points_16_av)
bysort group_id: egen points_17_av_tot=total(points_17_av)
bysort group_id: egen points_18_av_tot=total(points_18_av)
bysort group_id: egen points_19_av_tot=total(points_19_av)
bysort group_id: egen points_20_av_tot=total(points_20_av)

bysort group_id: egen points_0_fptp_tot=total(points_0_fptp)
bysort group_id: egen points_1_fptp_tot=total(points_1_fptp)
bysort group_id: egen points_2_fptp_tot=total(points_2_fptp)
bysort group_id: egen points_3_fptp_tot=total(points_3_fptp)
bysort group_id: egen points_4_fptp_tot=total(points_4_fptp)
bysort group_id: egen points_5_fptp_tot=total(points_5_fptp)
bysort group_id: egen points_6_fptp_tot=total(points_6_fptp)
bysort group_id: egen points_7_fptp_tot=total(points_7_fptp)
bysort group_id: egen points_8_fptp_tot=total(points_8_fptp)
bysort group_id: egen points_9_fptp_tot=total(points_9_fptp)
bysort group_id: egen points_10_fptp_tot=total(points_10_fptp)
bysort group_id: egen points_11_fptp_tot=total(points_11_fptp)
bysort group_id: egen points_12_fptp_tot=total(points_12_fptp)
bysort group_id: egen points_13_fptp_tot=total(points_13_fptp)
bysort group_id: egen points_14_fptp_tot=total(points_14_fptp)
bysort group_id: egen points_15_fptp_tot=total(points_15_fptp)
bysort group_id: egen points_16_fptp_tot=total(points_16_fptp)
bysort group_id: egen points_17_fptp_tot=total(points_17_fptp)
bysort group_id: egen points_18_fptp_tot=total(points_18_fptp)
bysort group_id: egen points_19_fptp_tot=total(points_19_fptp)
bysort group_id: egen points_20_fptp_tot=total(points_20_fptp)

* Dividing by 4 because points are counted four times

replace points_0_av_tot=points_0_av_tot/4
replace points_1_av_tot=points_1_av_tot/4
replace points_2_av_tot=points_2_av_tot/4
replace points_3_av_tot=points_3_av_tot/4
replace points_4_av_tot=points_4_av_tot/4
replace points_5_av_tot=points_5_av_tot/4
replace points_6_av_tot=points_6_av_tot/4
replace points_7_av_tot=points_7_av_tot/4
replace points_8_av_tot=points_8_av_tot/4
replace points_9_av_tot=points_9_av_tot/4
replace points_10_av_tot=points_10_av_tot/4
replace points_11_av_tot=points_11_av_tot/4
replace points_12_av_tot=points_12_av_tot/4
replace points_13_av_tot=points_13_av_tot/4
replace points_14_av_tot=points_14_av_tot/4
replace points_15_av_tot=points_15_av_tot/4
replace points_16_av_tot=points_16_av_tot/4
replace points_17_av_tot=points_17_av_tot/4
replace points_18_av_tot=points_18_av_tot/4
replace points_19_av_tot=points_19_av_tot/4
replace points_20_av_tot=points_20_av_tot/4

replace points_0_fptp_tot=points_0_fptp_tot/4
replace points_1_fptp_tot=points_1_fptp_tot/4
replace points_2_fptp_tot=points_2_fptp_tot/4
replace points_3_fptp_tot=points_3_fptp_tot/4
replace points_4_fptp_tot=points_4_fptp_tot/4
replace points_5_fptp_tot=points_5_fptp_tot/4
replace points_6_fptp_tot=points_6_fptp_tot/4
replace points_7_fptp_tot=points_7_fptp_tot/4
replace points_8_fptp_tot=points_8_fptp_tot/4
replace points_9_fptp_tot=points_9_fptp_tot/4
replace points_10_fptp_tot=points_10_fptp_tot/4
replace points_11_fptp_tot=points_11_fptp_tot/4
replace points_12_fptp_tot=points_12_fptp_tot/4
replace points_13_fptp_tot=points_13_fptp_tot/4
replace points_14_fptp_tot=points_14_fptp_tot/4
replace points_15_fptp_tot=points_15_fptp_tot/4
replace points_16_fptp_tot=points_16_fptp_tot/4
replace points_17_fptp_tot=points_17_fptp_tot/4
replace points_18_fptp_tot=points_18_fptp_tot/4
replace points_19_fptp_tot=points_19_fptp_tot/4
replace points_20_fptp_tot=points_20_fptp_tot/4

* Grouping to two symmetric position, and dividing by 2

gen sum_points_0_20_av=(points_0_av_tot+points_20_av_tot)/2
gen sum_points_1_19_av=(points_1_av_tot+points_19_av_tot)/2
gen sum_points_2_18_av=(points_2_av_tot+points_18_av_tot)/2
gen sum_points_3_17_av=(points_3_av_tot+points_17_av_tot)/2
gen sum_points_4_16_av=(points_4_av_tot+points_16_av_tot)/2
gen sum_points_5_15_av=(points_5_av_tot+points_15_av_tot)/2
gen sum_points_6_14_av=(points_6_av_tot+points_14_av_tot)/2
gen sum_points_7_13_av=(points_7_av_tot+points_13_av_tot)/2
gen sum_points_8_12_av=(points_8_av_tot+points_12_av_tot)/2
gen sum_points_9_11_av=(points_9_av_tot+points_11_av_tot)/2
gen sum_points_10_10_av=(points_10_av_tot)

gen sum_points_0_20_fptp=(points_0_fptp_tot+points_20_fptp_tot)/2
gen sum_points_1_19_fptp=(points_1_fptp_tot+points_19_fptp_tot)/2
gen sum_points_2_18_fptp=(points_2_fptp_tot+points_18_fptp_tot)/2
gen sum_points_3_17_fptp=(points_3_fptp_tot+points_17_fptp_tot)/2
gen sum_points_4_16_fptp=(points_4_fptp_tot+points_16_fptp_tot)/2
gen sum_points_5_15_fptp=(points_5_fptp_tot+points_15_fptp_tot)/2
gen sum_points_6_14_fptp=(points_6_fptp_tot+points_14_fptp_tot)/2
gen sum_points_7_13_fptp=(points_7_fptp_tot+points_13_fptp_tot)/2
gen sum_points_8_12_fptp=(points_8_fptp_tot+points_12_fptp_tot)/2
gen sum_points_9_11_fptp=(points_9_fptp_tot+points_11_fptp_tot)/2
gen sum_points_10_10_fptp=(points_10_fptp_tot)

* Taking the difference between approval and fptp, and assigning it to the subject's position in the second part

gen benefit_rational=.
replace benefit_rational=sum_points_0_20_av-sum_points_0_20_fptp if subjectposition==0 & period>8
replace benefit_rational=sum_points_1_19_av-sum_points_1_19_fptp if subjectposition==1 & period>8
replace benefit_rational=sum_points_2_18_av-sum_points_2_18_fptp if subjectposition==2 & period>8
replace benefit_rational=sum_points_3_17_av-sum_points_3_17_fptp if subjectposition==3 & period>8
replace benefit_rational=sum_points_4_16_av-sum_points_4_16_fptp if subjectposition==4 & period>8
replace benefit_rational=sum_points_5_15_av-sum_points_5_15_fptp if subjectposition==5 & period>8
replace benefit_rational=sum_points_6_14_av-sum_points_6_14_fptp if subjectposition==6 & period>8
replace benefit_rational=sum_points_7_13_av-sum_points_7_13_fptp if subjectposition==7 & period>8
replace benefit_rational=sum_points_8_12_av-sum_points_8_12_fptp if subjectposition==8 & period>8
replace benefit_rational=sum_points_9_11_av-sum_points_9_11_fptp if subjectposition==9 & period>8
replace benefit_rational=sum_points_10_10_av-sum_points_10_10_fptp if subjectposition==10 & period>8
replace benefit_rational=sum_points_0_20_av-sum_points_0_20_fptp if subjectposition==20 & period>8
replace benefit_rational=sum_points_1_19_av-sum_points_1_19_fptp if subjectposition==19 & period>8
replace benefit_rational=sum_points_2_18_av-sum_points_2_18_fptp if subjectposition==18 & period>8
replace benefit_rational=sum_points_3_17_av-sum_points_3_17_fptp if subjectposition==17 & period>8
replace benefit_rational=sum_points_4_16_av-sum_points_4_16_fptp if subjectposition==16 & period>8
replace benefit_rational=sum_points_5_15_av-sum_points_5_15_fptp if subjectposition==15 & period>8
replace benefit_rational=sum_points_6_14_av-sum_points_6_14_fptp if subjectposition==14 & period>8
replace benefit_rational=sum_points_7_13_av-sum_points_7_13_fptp if subjectposition==13 & period>8
replace benefit_rational=sum_points_8_12_av-sum_points_8_12_fptp if subjectposition==12 & period>8
replace benefit_rational=sum_points_9_11_av-sum_points_9_11_fptp if subjectposition==11 & period>8

* Creating a binary version of the benefit_rational variable
*************************************************************

gen benefit_rational_dum=1 if benefit_rational>0
replace benefit_rational_dum=0 if benefit_rational<0


* Calculating how extreme is the subject
*****************************************

gen extreme=abs(10-subjectposition)

* Creatign dependent variable: support for approval voting (vs first past the post)
***********************************************************************************

gen approval=.
replace approval=1 if systemchoice=="2"
replace approval=0 if systemchoice=="1"

* Creating the veil variable
****************************

gen veil=0
replace veil=1 if period==10 & session==1
replace veil=1 if period==9 & session==2
replace veil=1 if period==10 & session==3
replace veil=1 if period==9 & session==4
replace veil=1 if period==10 & session==1
replace veil=1 if period==9 & session==2
replace veil=1 if period==10 & session==3
replace veil=1 if period==9 & session==4
replace veil=1 if period==10 & session==5
replace veil=1 if period==9 & session==6
replace veil=1 if period==10 & session==7
replace veil=1 if period==9 & session==8
replace veil=1 if period==10 & session==9
replace veil=1 if period==9 & session==10
replace veil=1 if period==10 & session==11
replace veil=1 if period==9 & session==12
replace veil=. if period<9

* Creating the index of egalitarianism
**************************************

* De-stringing variable

gen egal1=real(egalitarism1)
gen egal2=real(egalitarism2)
gen egal3=real(egalitarism3)
gen egal4=real(egalitarism4)

* Replacing missing value

replace egal1=. if period!=10 
replace egal2=. if period!=10
replace egal3=. if period!=10
replace egal4=. if period!=10

* Assigning the value at all period

bysort group_id: egen egal1_tot=total(egal1)
bysort group_id: egen egal2_tot=total(egal2)
bysort group_id: egen egal3_tot=total(egal3)
bysort group_id: egen egal4_tot=total(egal4)

* Re-scaling variable in the good direction

replace egal1_tot=egal1_tot-1
replace egal2_tot=4-(egal2_tot-1)
replace egal3_tot=4-(egal3_tot-1)
replace egal4_tot=4-(egal4_tot-1)

* Correlaton and alpha cronbach

corr egal1_tot egal2_tot egal3_tot egal4_tot if period==9
alpha egal1_tot egal2_tot egal3_tot egal4_tot if period==9, a
alpha egal1_tot egal2_tot if period==9, a

* Creating the index

gen egal_index=egal1_tot+egal2_tot

* Creating the quiz responses
*****************************

* De-stringing variable

gen quiz1=real(quiz1_q1)
gen quiz2=real(quiz1_q2)
gen quiz3=real(quiz2_q1)
gen quiz4=real(quiz2_q2)

replace quiz1=. if quiz1==0
replace quiz2=. if quiz2==0
replace quiz3=. if quiz3==0
replace quiz4=. if quiz4==0


* Creating socio-demographic and attitude control variables
***********************************************************

* Gender

gen male_r=real(male)
replace male_r=. if period!=10
bysort group_id: egen male_tot=total(male_r)

* Age

gen age_r=real(age)
replace age_r=. if period!=10
bysort group_id: egen age_tot=total(age_r)

* Political Interest

gen polinterest_r=real(politicalinterest)
replace polinterest_r=. if period!=10
bysort group_id: egen polinterest_tot=total(polinterest_r)

* Creating perceived fairness of rules
**************************************

* De-stringing variable

gen fair_approval2=real(fair_approval)
gen fair_plurality2=real(fair_plurality)

replace fair_approval2=real(thrill_approval) if uk==1
replace fair_plurality2=real(thrill_plurality) if uk==1

* Assigning the value at all period

bysort group_id: egen fair_approval_tot=total(fair_approval2)
bysort group_id: egen fair_plurality_tot=total(fair_plurality2)

* Creating a first continuous variable

gen fair=(fair_approval_tot-fair_plurality_tot)

* Creating a second dummy variable

gen fair2=0
replace fair2=-1 if fair<0
replace fair2=1 if fair>0

* Creating variables capturing how egalitarians are the rules
*************************************************************

* Creating standard deviation of payoff distributions

bysort session: sum points_av_tot points_fptp_tot if period==1

gen std_av=.
gen std_fptp=.

replace std_av=10.01142  if session==1
replace std_fptp=11.88516 if session==1

replace std_av=10.60458 if session==2
replace std_fptp=10.60458 if session==2

replace std_av=10.01142 if session==3
replace std_fptp=11.88516 if session==3

replace std_av=9.519904 if session==4
replace std_fptp=12.87301 if session==4

replace std_av=10.01142 if session==5
replace std_fptp=11.88516 if session==5

replace std_av=9.519904  if session==6
replace std_fptp=11.88516 if session==6

replace std_av=9.519904 if session==7
replace std_fptp=10.60458 if session==7

replace std_av=9.519904 if session==8
replace std_fptp=10.60458 if session==8

replace std_av=7.17834 if session==9
replace std_fptp=9.519904 if session==9

replace std_av=11.88516 if session==10
replace std_fptp=11.88516 if session==10

replace std_av=10.01142 if session==11
replace std_fptp=11.88516 if session==11

replace std_av=10.01142 if session==12
replace std_fptp=9.519904 if session==12

gen std=std_av-std_fptp

* Creating average number of points given to subjects

bysort session: sum points_av_tot points_fptp_tot if period==1

gen mean_av=.
gen mean_fptp=.

replace mean_av=15.14286 if session==1
replace mean_fptp=15.42857 if session==1

replace mean_av=14.57143 if session==2
replace mean_fptp=14.57143 if session==2

replace mean_av=15.14286 if session==3
replace mean_fptp=15.42857 if session==3

replace mean_av=14.85714 if session==4
replace mean_fptp=14.28571 if session==4

replace mean_av=15.14286 if session==5
replace mean_fptp=15.42857 if session==5

replace mean_av=14.85714 if session==6
replace mean_fptp=15.42857 if session==6

replace mean_av=14.85714 if session==7
replace mean_fptp=14.57143 if session==7

replace mean_av=14.85714 if session==8
replace mean_fptp=14.57143 if session==8

replace mean_av=14.85714 if session==9
replace mean_fptp=14.85714 if session==9

replace mean_av=15.42857 if session==10
replace mean_fptp=15.42857 if session==10

replace mean_av=15.14286 if session==11
replace mean_fptp=15.42857 if session==11

replace mean_av=15.14286 if session==12
replace mean_fptp=14.85714 if session==12

gen mean=mean_av-mean_fptp

* Creating number of subjects with at least 1 point by session

bysort session: list subject if points_av_tot==0 & period==1
bysort session: list subject if points_fptp_tot==0 & period==1

gen point_av=.
gen point_fptp=.

replace point_av=(21-2) if session==1
replace point_fptp=(21-4) if session==1

replace point_av=(21-2) if session==2
replace point_fptp=(21-2) if session==2

replace point_av=(21-2) if session==3
replace point_fptp=(21-4) if session==3

replace point_av=(21-2) if session==4
replace point_fptp=(21-4) if session==4

replace point_av=(21-2) if session==5
replace point_fptp=(21-7) if session==5

replace point_av=(21-2) if session==6
replace point_fptp=(21-4) if session==6

replace point_av=(21-2) if session==7
replace point_fptp=(21-2) if session==7

replace point_av=(21-2) if session==8
replace point_fptp=(21-2) if session==8

replace point_av=(21-0) if session==9
replace point_fptp=(21-2) if session==9

replace point_av=(21-4) if session==10
replace point_fptp=(21-4) if session==10

replace point_av=(21-2) if session==11
replace point_fptp=(21-4) if session==11

replace point_av=(21-2) if session==12
replace point_fptp=(21-2) if session==12

gen point=point_av-point_fptp

* Creating min number of points

bysort session: sum points_av_tot points_fptp_tot if period==1

gen min_point_av=0
gen min_point_fptp=0
replace min_point_av=4 if session==9

gen min_point=min_point_av-min_point_fptp

* Creating variable capturing whether subjects change their vote
*****************************************************************

gen vote1=.
gen vote2=.
replace vote1=approval if period==9
replace vote2=approval if period==10

bysort session subject: egen vote1_tot=total(vote1)
bysort session subject: egen vote2_tot=total(vote2)

gen change=0
replace change=1 if vote1_tot!=vote2_tot

gen approval_veil=0
replace approval_veil=1 if veil==1 & approval==1

gen approval_noveil=0
replace approval_noveil=1 if veil==0 & approval==1

bysort group_id: egen approval_veil_tot=total(approval_veil)
bysort group_id: egen approval_noveil_tot=total(approval_noveil)

* Creating variable identifying election order in the first phase
******************************************************************

gen election=.
replace election=period if period<5
replace election=period-4 if period>4

* Creating variable margin of victory
*************************************

* Identifying first score
gen first_score=max(suma, sumb, sumc, sumd, sume)

gen suma_b=suma
gen sumb_b=sumb
gen sumc_b=sumc
gen sumd_b=sumd
gen sume_b=sume

replace suma_b=0 if suma_b==first_score
replace sumb_b=0 if sumb_b==first_score
replace sumc_b=0 if sumc_b==first_score
replace sumd_b=0 if sumd_b==first_score
replace sume_b=0 if sume_b==first_score

* Identifying second score

gen second_score=max(suma_b, sumb_b, sumc_b, sumd_b, sume_b)

* Creating margin of victory variable

gen margin=first_score-second_score
replace margin=0 if tie==1

************
* ANALYSIS *
************

* First stage
**************

* In the text: Alpha cronbach

alpha egal1_tot egal2_tot if period==9, a

* In the text: Correlation between egalitarian values and egalitarianism of the session

pwcorr egal_index benefit_rational if period>8, sig

* In the text: Correlation between egalitartian values and gains

bysort session subject: egen total_gain=total(gain)
pwcorr total_gain egal_index if period==1, sig

* Table A2A

sum suma sumb sumc sumd sume if period<9 & subject==1 & av==0
sum suma sumb sumc sumd sume if period<9 & subject==1 & av==1
tab av winner if period<9 & subject==1, ro

* Table A1B

tab quiz1
tab quiz2
tab quiz3
tab quiz4

* Table A1D

sum age_tot male_tot if period==1 & uk==1
sum age_tot male_tot if period==1 & uk==0

* Table 2

sum std_av std_fptp point_av point_fptp mean_av mean_fptp min_point_av min_point_fptp if period==1 & subject==1

ttest std_av==std_fptp if period==1 & subject==1
ttest point_av==point_fptp if period==1 & subject==1
ttest mean_av==mean_fptp if period==1 & subject==1
ttest min_point_av==min_point_fptp if period==1 & subject==1

* In the text: Margin of victory

bysort av: sum margin if period<9
bysort av: sum margin if election==1 & period<9
bysort av: sum margin if election==4 & period<9

* In the text: Percevied fairness

tab fair2 if period==1
tab fair2 if session==6 & period==1
tab fair2 if session==10 & period==1

* Second stage
**************

* In the text: Dependent variable

tab veil approval, ro
tab uk approval, ro

gen av_first=0
replace av_first=1 if period==1 & av==1
bysort session subject: egen total_av_first=total(av_first)

tab total_av_first approval, ro


* Table A2B

sum approval benefit_rational egal_index veil benefit_behavioral mean uk age_tot male_tot polinterest_tot if period>8

* Table 4: main results

logit approval benefit_rational egal_index if veil==0, vce(cluster session)
margins, dydx(*) post
outreg2 using table4.doc, se dec(3) alpha(0.01, 0.05)  replace

logit approval benefit_rational egal_index if veil==1, vce(cluster session)
margins, dydx(*) post
outreg2 using table4.doc, se dec(3) alpha(0.01, 0.05) append

gen inter1=benefit_rational*veil
gen inter2=egal_index*veil

logit approval benefit_rational egal_index inter1 inter2 veil, vce(cluster session)
margins, dydx(*) post
outreg2 using table4.doc, se dec(3) alpha(0.01, 0.05) append

logit approval c.benefit_rational##c.veil c.egal_index##c.veil, vce(cluster session) // Simulation in the text
margins, dydx(egal_index) at(veil=1) post

* Table 4: goodness of fit

logit approval benefit_rational egal_index if veil==0, vce(cluster session)
estat gof

logit approval benefit_rational egal_index if veil==1, vce(cluster session)
estat gof

logit approval benefit_rational egal_index inter1 inter2 veil, vce(cluster session)
estat gof


* In the text: Bivariate analysis

ttest egal_index, by(approval)
ttest egal_index if veil==1, by(approval)
ttest egal_index if veil==0, by(approval) 


* In the text: change of choice

tab change if period==1

ttest egal_index if approval_noveil_tot==1 & period==9, by(approval_veil_tot)
ttest egal_index if approval_noveil_tot==0 & period==9, by(approval_veil_tot)

* In A2C: Regression with control variables

corr egal_index uk age_tot male_tot polinterest_tot 

logit approval benefit_rational egal_index benefit_behavioral  mean uk age_tot male_tot polinterest_tot if veil==0, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2c.doc, se dec(3) alpha(0.01, 0.05)  replace

logit approval benefit_rational egal_index benefit_behavioral mean uk  age_tot male_tot polinterest_tot if veil==1, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2c.doc, se dec(3) alpha(0.01, 0.05)  append

logit approval benefit_rational egal_index inter1 inter2 veil benefit_behavioral  mean uk age_tot male_tot polinterest_tot, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2c.doc, se dec(3) alpha(0.01, 0.05)  append

* In A2D: OLS regressions

reg approval benefit_rational egal_index if veil==0, vce(cluster session)
outreg2 using tablea2d.doc, se dec(3) alpha(0.01, 0.05)  replace

reg approval benefit_rational egal_index if veil==1, vce(cluster session)
outreg2 using tablea2d.doc, se dec(3) alpha(0.01, 0.05)  append

reg approval benefit_rational egal_index inter1 inter2 veil, vce(cluster session)
outreg2 using tablea2d.doc, se dec(3) alpha(0.01, 0.05)  append

* Figure 1

sum benefit_rational egal_index

quietly logit approval benefit_rational egal_index if veil==0, vce(cluster session)
margins, at(benefit_rational=(-10(2)10)) atmeans
quietly marginsplot, recast(line) recastci(rarea) title("No Veil") graphregion(color(white)) ylabel(0(0.2)1) xlabel(-10(4)10) xtitle("Rational Benefit") ytitle("Pr(Choosing Approval)") scheme(s2mono) addplot(hist benefit_rational, frac lcolor(gs12) color(none) ylabel(0(0.2)1) xlabel(-10(4)10)) legend(off)
graph save rat_no.gph, replace

quietly logit approval benefit_rational egal_index if veil==0, vce(cluster session)
 margins, at(egal_index=(0(1)8)) atmeans
quietly marginsplot, recast(line) recastci(rarea) title("No Veil") graphregion(color(white)) ylabel(0(0.2)1) xlabel(0(1)8) xtitle("Egalitarianism") ytitle("Pr(Choosing Approval)") scheme(s2mono) addplot(hist egal_index, frac lcolor(gs12) color(none) ylabel(0(0.2)1) xlabel(0(1)8)) legend(off)
graph save egal_no.gph, replace

quietly logit approval benefit_rational egal_index if veil==1, vce(cluster session)
margins, at(egal_index=(0(1)8)) atmeans
quietly marginsplot, recast(line) recastci(rarea) title("Veil") graphregion(color(white)) ylabel(0(0.2)1) xlabel(0(1)8) xtitle("Egalitarianism") ytitle("Pr(Choosing Approval)") scheme(s2mono) addplot(hist egal_index, frac lcolor(gs12) color(none) ylabel(0(0.2)1) xlabel(0(1)8)) legend(off)
graph save egal_veil.gph, replace

graph combine rat_no.gph egal_no.gph egal_veil.gph, row(1) graphregion(color(white))

* Figure 2

quietly logit approval benefit_rational c.egal_index##c.point if veil==1, vce (cluster session)
margins, dydx(egal_index) atmeans at(point=(0(1)5))
marginsplot, recast(line) recastci(rline) ci(lp(dash)) yline(0,lp(dot))title("Veil") graphregion(color(white)) xlabel(0(1)5) ylabel(-.1(0.1).4) xtitle("Nbr. Subjects With 1+ Point (Approval - Plurality)") ytitle("Marginal Effect of Egalitarianism") scheme(s2mono) 
graph save mech_veil.gph, replace

quietly logit approval  benefit_rational c.egal_index##c.point if veil==0, vce(cluster session)
margins, dydx(egal_index) atmeans at(point=(0(1)5))
marginsplot, recast(line) recastci(rline) ci(lp(dash)) yline(0,lp(dot))title("No Veil") graphregion(color(white)) xlabel(0(1)5) ylabel(-.1(0.1).4) xtitle("Nbr. Subjects With 1+ Point (Approval - Plurality)") ytitle("Marginal Effect of Egalitarianism") scheme(s2mono) 
graph save mech_noveil.gph, replace

graph combine mech_noveil.gph mech_veil.gph, row(1) graphregion(color(white))

* In A2E: Complete regression results

replace inter1=egal_index*std
replace inter2=egal_index*point

logit approval  benefit_rational egal_index std inter1 if veil==0, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2e.doc, se dec(3) alpha(0.01, 0.05)  replace

logit approval  benefit_rational egal_index std inter1 if veil==1, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2e.doc, se dec(3) alpha(0.01, 0.05)  append

logit approval  benefit_rational egal_index point inter2 if veil==0, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2e.doc, se dec(3) alpha(0.01, 0.05)  append

logit approval  benefit_rational egal_index point inter2 if veil==1, vce(cluster session)
margins, dydx(*) post
outreg2 using tablea2e.doc, se dec(3) alpha(0.01, 0.05)  append

* Power analysis
*****************

sum egal_index // (-1 SD = 3.694246; +1 SD 7.31369)

logit approval benefit_rational egal_index if veil==0, vce(cluster session)
margins, at(egal_index=5.503968 egal_index=7.31369) atmeans
powerlog, p1(0.5567913) p2(0.6358749) alpha(0.01)

logit approval benefit_rational egal_index if veil==1, vce(cluster session)
margins, at(egal_index=5.503968 egal_index=7.31369) atmeans
powerlog, p1(0.617931 ) p2(0.6940767) alpha(0.01)


* Extra analysis: Regressions predicting votes (Table 3)
********************************************************

* Creating an id variable

gen id = _n

* Renaming approval variables

rename approvala approval_vote1
rename approvalb approval_vote2
rename approvalc approval_vote3
rename approvald approval_vote4
rename approvale approval_vote5

* Reshaping based on vote under approval

reshape long approval_vote, i(id) j(cand) 

* Creating variable capturing vote under fpfp

gen fptp=.
replace fptp=0
replace fptp=1 if cand==1 & votefptp==1
replace fptp=1 if cand==2 & votefptp==2
replace fptp=1 if cand==3 & votefptp==3
replace fptp=1 if cand==4 & votefptp==4
replace fptp=1 if cand==5 & votefptp==5

* Cleaning up vote variables

replace fptp=. if period>8
replace approval_vote=. if period>8

replace fptp=. if av==1
replace approval_vote=. if av==0

* Calculating distance to candidate

gen distance=.
replace distance=abs(subjectposition-0) if cand==1
replace distance=abs(subjectposition-5) if cand==2
replace distance=abs(subjectposition-10) if cand==3
replace distance=abs(subjectposition-15) if cand==4
replace distance=abs(subjectposition-20) if cand==5

* Conditional logit regressions

clogit fptp distance ib(3).cand, group(id) vce(cluster group_id)
margins, dydx(*) atmeans post
outreg2 using table3.doc, se dec(3) alpha(0.01, 0.05, 0.1)  replace

clogit approval_vote distance ib(3).cand, group(id)  vce(cluster group_id)
margins, dydx(*) atmeans post
outreg2 using table3.doc, se dec(3) alpha(0.01, 0.05, 0.1)  append
